package com.tqz.rc.spring.cloud.discovery;

import com.tqz.rc.common.exception.RcException;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

import java.util.List;

/**
 * <p>
 *
 * @author tianqingzhao
 * @since 2021/9/3 14:57
 */
public class TqzRcDiscoveryClient implements DiscoveryClient {

    private TqzRcServiceDiscovery tqzRcServiceDiscovery;

    /**
     * 服务发现的描述。
     */
    private static final String DESCRIPTION = "Spring Cloud TqzRc Discovery Client.";

    public TqzRcDiscoveryClient(TqzRcServiceDiscovery tqzRcServiceDiscovery) {
        this.tqzRcServiceDiscovery = tqzRcServiceDiscovery;
    }

    @Override
    public String description() {
        return DESCRIPTION;
    }

    @Override
    public List<ServiceInstance> getInstances(String serviceId) {
        try {
            return tqzRcServiceDiscovery.getInstances(serviceId);
        } catch (RcException e) {
            throw new RuntimeException(
                    "Can not get hosts from TqzRc server. serviceId: " + serviceId, e);
        }
    }

    @Override
    public List<String> getServices() {
        return tqzRcServiceDiscovery.getServices();
    }


}
